<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Layout {
    /* VARS */

    private $_CI;
    private $_vars = array();
    private $_theme = 'plastprod';

    /* PUBLIC METHODS */

    public function __construct() {
        //links_header_model
        $this->_CI = & get_instance();
        $this->_CI->load->model('links_header_model');
        $this->_vars['output'] = '';
        $this->_vars['charset'] = $this->_CI->config->item('charset');
        $this->_vars['linksHeader'] = $this->_CI->links_header_model->get();
        $this->_vars['css'] = array();
        $this->_vars['js'] = array();
        $this->_vars['menus'] = $this->_loadMenus();

        $this->setCss('theme/plastprod');
        $this->setCss('jquery.noty');
        $this->setCss('jquery-ui/smoothness/jquery-ui-1.9.2.custom.min');
        $this->setJs('jQuery/jquery-1.8.3');
        $this->setJs('jQuery/add-on/jquery-ui-1.9.2.custom');
        $this->setJs('jquery.noty');
        $this->setJs('main');
    }

    public function display() {
        $this->_CI->load->view('../themes/' . $this->_theme, $this->_vars);
    }

    public function &setCharset($charset) {
        if (is_string($charset) AND !empty($charset))
            $this->_vars['charset'] = $charset;
        return $this;
    }

    public function &setCss($css) {
        if (is_string($css) && !empty($css) && file_exists('./assets/css/' . $css . '.css'))
            $this->_vars['css'][] = css_url($css);
        else if (is_array($css))
            $this->_vars['css'] = array_merge($this->_vars['css'], $css);

        return $this;
    }

    public function &setJs($js) {
        if (is_string($js) && !empty($js) && file_exists('./assets/javascript/' . $js . '.js'))
            $this->_vars['js'][] = js_url($js);
        else if (is_array($js))
            $this->_vars['js'] = array_merge($this->_vars['js'], $js);

        return $this;
    }

    public function &setTitle($title) {
        if (is_string($title) && !empty($title))
            $this->_vars['title'] = $title;

        return $this;
    }

    public function &view($name, $data = array()) {
        $this->_vars['output'] .= $this->_CI->load->view($name, $data, true);
        return $this;
    }

    public function _loadMenus() {
        $fields = array(
            '"S_INTRANET"."V_MENU_MNU"."MNU_ID"',
            '"S_INTRANET"."V_MENU_MNU"."MNU_NAME"'
        );

        $this->_CI->db = $this->_CI->load->database('DB_PLASTPROD', true);
        $tmpMenus = $this->_CI->db
                ->select(implode(', ', $fields))
                ->from('"S_INTRANET"."V_MENU_MNU"')
                ->order_by('"S_INTRANET"."V_MENU_MNU"."MNU_POSITION"', 'ASC')
                ->get()
                ->result()
        ;

        $menus = array();
        foreach ($tmpMenus AS $id => $menu) {
            $menus[$id]['name'] = $menu->MNU_NAME;
            $menus[$id]['id'] = 'menu-' . $menu->MNU_ID;
            $menus[$id]['links'] = array();

            $fields = array(
                '"S_INTRANET"."V_LINK_LNK"."LNK_ID"',
                '"S_INTRANET"."V_LINK_LNK"."LNK_NAME"',
                '"S_INTRANET"."V_LINK_LNK"."LNK_LINK"'
            );

            $this->_CI->db = $this->_CI->load->database('DB_PLASTPROD', true);
            $tmpLinks = $this->_CI->db
                    ->select(implode(', ', $fields))
                    ->from('"S_INTRANET"."V_LINK_LNK"')
                    ->where('"S_INTRANET"."V_LINK_LNK"."MNU_ID"', $menu->MNU_ID)
                    ->order_by('"S_INTRANET"."V_LINK_LNK"."LNK_POSITION"', 'ASC')
                    ->get()
                    ->result()
            ;

            foreach ($tmpLinks AS $idLnk => $link) {
                $menus[$id]['links'][$idLnk]['name'] = $link->LNK_NAME;
                $menus[$id]['links'][$idLnk]['id'] = 'link-' . $link->LNK_ID;
                $menus[$id]['links'][$idLnk]['link'] = $link->LNK_LINK;
            }
        }

        return $menus;
    }

}

?>